from fastapi import APIRouter, Depends, HTTPException, Request, Form
from sqlalchemy.orm import Session

from app.utils.resp_util import APIResponse

from app.extensions.dependencies import get_query_token2
from app.dependencies.db import get_db

from app.schemas import factor as schemas
from app.services import factor as factor_service

import logging

logger = logging.getLogger(__name__)

router = APIRouter(prefix='/factor', tags=['factor'])
#router = APIRouter(dependencies=[Depends(get_query_token2)],)


@router.get("/list1", response_model=list[schemas.FactorDetail])
async def factor_list1(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    logger.debug('factor_list1')
    return factor_service.get_factors(db)

@router.get("/list", response_model=APIResponse[list[schemas.FactorDetail]])
async def factor_list(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    logger.debug('>> factor_list')
    factors = factor_service.get_factors(db)
    resp = APIResponse[list[schemas.FactorDetail]](data=factors)
    return resp

@router.get("/{user_id}", response_model=schemas.FactorDetail)
def get_factor(factor_no: str, db: Session = Depends(get_db)):
    factor = factor_service.get_factor(db, factor_no=factor_no)
    if factor is None:
        raise HTTPException(status_code=404, detail="factorhant not found")
    return factor